package com.xuecheng.order.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.api.order.model.dto.OrdersDTO;
import com.xuecheng.api.order.model.vo.PayResultModel;
import com.xuecheng.common.enums.order.OrderDealStatusEnum;
import com.xuecheng.common.util.JsonUtil;
import com.xuecheng.order.entity.Orders;
import com.xuecheng.order.entity.Pay;
import com.xuecheng.order.service.OrdersService;
import com.xuecheng.order.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
@Slf4j
@RocketMQTransactionListener
public class ProducerTxmsgPayListener implements RocketMQLocalTransactionListener {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private PayService payService;

    /**
     * 1.解析消息并封装
     * 2.调用service层执行本地事务
     * 3.返回结果给mq
     * commit/Rollback
     */
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        try {
            //1.解析消息并封装
            String jsonStr = new String((byte[]) message.getPayload());
            PayResultModel resultModel = JsonUtil.jsonToObject(jsonStr, PayResultModel.class);
            //2.调用service层执行本地事务
            ordersService.successPayment(resultModel);
            //3.返回结果给mq
            //commit/Rollback
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            log.error("事务监听器执行本地事务失败: {}", e.getMessage());
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }


    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        //1.解析消息并封装
        String jsonStr = new String((byte[]) message.getPayload());
        PayResultModel resultModel = JsonUtil.jsonToObject(jsonStr, PayResultModel.class);
        //判断本地事务执行结果
        OrdersDTO ordersDTO = resultModel.getOrder();
        Orders orders = ordersService.getById(ordersDTO.getOrderId());
        if (ObjectUtils.isEmpty(orders)) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        //根据orderId获得订单支付数据
        LambdaQueryWrapper<Pay> payQueryWrapper = new LambdaQueryWrapper<>();
        payQueryWrapper.eq(Pay::getOrderId, orders.getId());

        Pay pay = payService.getOne(payQueryWrapper);
        if (ObjectUtils.isEmpty(pay)) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        //判断订单数据状态及订单支付状态  是否支付
        if (!(OrderDealStatusEnum.ORDER_DEAL_PAID_STATUS.getCode().equals(orders.getStatus().toString()))) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        if (!(Pay.IS_PAY.equals(pay.getStatus()))) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }

        return RocketMQLocalTransactionState.COMMIT;
    }
}
